package cn.edu.scau.cmi.linkaibo.client;
import cn.edu.scau.cmi.linkaibo.adapter.*;
import java.util.*;
import javafx.application.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.*;
import javafx.stage.*;
import javafx.event.*;

public class ClassAdapterClient extends Application{

	public static void main(String[] args) {
		launch(args); // 启动JavaFX应用，接下来会跳到start方法
	}
	
	@Override
    public void start(Stage stage) { 
    	stage.setTitle("实验四（类适配器客户端）"); 
    	Pane pane=new Pane();
        AdapterGUI(pane);
        Scene scene = new Scene(pane, 700, 800); 
        stage.setScene(scene); // 设置舞台的场景
        stage.setResizable(false); // 设置舞台的尺寸是否允许变化
        stage.show(); // 显示舞台。相当于JFrame的setVisible(true)      
    }
	
    public void AdapterGUI(Pane pane) {
    	List<Integer> intList=new ArrayList<Integer>();
    	List<Student> students=new ArrayList<Student>();
    	
    	Label label1=new Label("请输入创建的对象的id:");
    	label1.setLayoutX(10);
    	label1.setLayoutY(10);
    	label1.setPrefSize(180, 30);
    	pane.getChildren().add(label1);
    	
    	Label label2=new Label("请输入创建的对象的姓名:");
    	label2.setLayoutX(10);
    	label2.setLayoutY(60);
    	label2.setPrefSize(180, 30); 
    	pane.getChildren().add(label2);
    	
    	TextField field1=new TextField(); 
	    field1.setLayoutX(200);
	    field1.setLayoutY(10); 
	    field1.setPrefSize(300,40);
	    pane.getChildren().add(field1); 
	    
	    TextField field2=new TextField(); 
	    field2.setLayoutX(200);
	    field2.setLayoutY(60); 
	    field2.setPrefSize(300,40);
	    pane.getChildren().add(field2);
	    
	    Label label3=new Label("创建的对象队列如下所示");
	    label3.setLayoutX(100);
	    label3.setLayoutY(140);
	    pane.getChildren().add(label3);
	    
	    TextArea textarea1=new TextArea();
        textarea1.setLayoutX(10);
        textarea1.setLayoutY(180);
        pane.getChildren().add(textarea1); 
        
        Label label4=new Label("是否将对象排序？");
        label4.setLayoutX(10);
        label4.setLayoutY(440);
        pane.getChildren().add(label4);
        
        TextArea textarea2=new TextArea();
        textarea2.setLayoutX(10);
        textarea2.setLayoutY(480);
        pane.getChildren().add(textarea2);
    	
    	Button button1=new Button("确定创建"); 
    	button1.setLayoutX(550); 
    	button1.setLayoutY(10);
    	button1.setPrefSize(80, 80);
    	button1.setOnAction(new EventHandler<ActionEvent>() { // 设置按钮的单击事件
    	    @Override
    	    public void handle(ActionEvent arg0) { // 处理单击事件   
    	       if(readTextField(intList,students,field1,field2,pane)==true)
    	       showObject(textarea1,field1.getText(),field2.getText());
    	       clearTextField(field1,field2); 
    	} 
    	}); 
    	pane.getChildren().add(button1); 
        
    	Button button2=new Button("YES");   
    	button2.setLayoutX(180); 
    	button2.setLayoutY(420);
    	button2.setPrefSize(80, 40);
    	button2.setOnAction(new EventHandler<ActionEvent>() { // 设置按钮的单击事件
    	    @Override
    	    public void handle(ActionEvent arg0) { // 处理单击事件   
    	    	 ClassAdapter adapter=new ClassAdapter();
    	         adapter.sort(intList, students);
    	         showStudentList(students,textarea2);
    	} 
    	});
        pane.getChildren().add(button2);  

    }
    public void showObject(TextArea textArea,String id,String name) {
    	if(id!=null&&name!=null) 
    	textArea.appendText("id="+id+","+"name="+name+"\n"); 
    }
    public void clearTextField(TextField t1,TextField t2) {
    	t1.clear();
    	t2.clear(); 
    } 
    public boolean readTextField(List<Integer> intList,List<Student> students,TextField t1,TextField t2,Pane pane) {
    	TextField textField=new TextField();
        if(t1.getText().equals("")||t2.getText().equals("")) {
        	textField.setText("请输入完整的信息！");
        	textField.setLayoutX(410); 
        	textField.setLayoutY(100);   
        	pane.getChildren().add(textField);
        	return false;
        }
	    String name=t2.getText();
	    int id=Integer.parseInt(t1.getText()); 
	    if(intList.contains(id)==true) {
	    	textField.setText("对象id重复，请重新输入！");
	    	textField.setLayoutX(410); 
	    	textField.setLayoutY(100);   
        	pane.getChildren().add(textField); 
        	return false;

	    }
	    intList.add(id);
	    students.add(new Student(id,name)); 
	    textField.setText("创建对象成功！");
	    textField.setLayoutX(410);  
	    textField.setLayoutY(100);   
     	pane.getChildren().add(textField); 
	    return true;
	}
    public void showStudentList(List<Student> students,TextArea textArea) {
    	for(int i=0; i<students.size(); i++) {
    		textArea.appendText("第"+(i+1)+"个对象:   "+"id="+students.get(i).getId()+","+"name="+students.get(i).getName()+"\n");
    	}
    }
}